﻿using System;

class AddTwoPolynomials
{
    private static void AddPolynomials(int[] firstInput, int[] secondInput)
    {
        int[] result = new int[Math.Max(firstInput.Length, secondInput.Length)];
        int[] firstPolynomial;
        int[] secondPolynomial;
        PreparePolynomials(firstInput, secondInput, out firstPolynomial, out secondPolynomial);

        for (int i = 0; i < result.Length; i++)
        {
            result[i] = firstPolynomial[i] + secondPolynomial[i];
        }

        Print(result);
    }

    private static void SubtractPolynomials(int[] firstInput, int[] secondInput)
    {
        int[] result = new int[Math.Max(firstInput.Length, secondInput.Length)];
        int[] firstPolynomial;
        int[] secondPolynomial;
        PreparePolynomials(firstInput, secondInput, out firstPolynomial, out secondPolynomial);

        for (int i = 0; i < result.Length; i++)
        {
            result[i] = firstPolynomial[i] - secondPolynomial[i];
        }

        Print(result);
    }

    private static void MultiplyPolynomials(int[] firstInput, int[] secondInput)
    {
        int[] firstPolynomial;
        int[] secondPolynomial;
        PreparePolynomials(firstInput, secondInput, out firstPolynomial, out secondPolynomial);
        int[] result = new int[(firstPolynomial.Length + secondPolynomial.Length) - 1];
        for (int i = 0; i < firstPolynomial.Length; i++)
        {
            for (int j = 0; j < secondPolynomial.Length; j++)
            {
                result[j+i] = firstPolynomial[i] * secondPolynomial[j] + result[j + i];
            }
        }
        Print(result);
    }

    private static void PreparePolynomials(int[] firstInput, int[] secondInput, out int[] firstPolynomial, out int[] secondPolynomial)
    {
        firstPolynomial = new int[Math.Max(firstInput.Length, secondInput.Length)];
        secondPolynomial = new int[Math.Max(firstInput.Length, secondInput.Length)];

        for (int i = 0; i < firstInput.Length; i++)
        {
            firstPolynomial[firstPolynomial.Length - i - 1] = firstInput[firstInput.Length - i - 1];
        }

        for (int i = 0; i < secondInput.Length; i++)
        {
            secondPolynomial[secondPolynomial.Length - i - 1] = secondInput[secondInput.Length - i - 1];
        }
    }

    static void Print(int[] list)
    {
        for (int i = 0; i < list.GetLength(0); i++)
        {
            Console.Write(list[i] + " ");
        }
        Console.WriteLine();
    }

    static void Main()
    {
        int[] firstPoly = {9,6,9};
        int[] secondPoly = { 9,2,34,6,12,76 };
        AddPolynomials(firstPoly, secondPoly);
        SubtractPolynomials(firstPoly, secondPoly);
        MultiplyPolynomials(firstPoly, secondPoly);
    }
}